﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System.Web;
using System.Configuration;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 切片上下机统计报表运算
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IWMSQPupdownInfoController : ControllerBase
    {
        private readonly ILogger<IWMSQPupdownInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_t;


        public IWMSQPupdownInfoController(ILogger<IWMSQPupdownInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_t = _configuration.GetConnectionString("WMSDbConnection_Test");

        }

        /// <summary>
        /// 切片上下机统计报表运算
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostQPupdown([FromBody] IWMSQPupdownInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            IWMSQPupdownInfoResponseDto re = new IWMSQPupdownInfoResponseDto();
            string sysid = "";

            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("machine_no", "");//查看URL配置表地址描述
                DataTable dt_u = MethodHelp.GetOutsideTableInfo("TMS0024", dic, con_t, con);//上下机记录列表
                if (dt_u.Rows.Count>0)
                {
                    foreach (DataRow item in dt_u.Rows)
                    {
                        string machine_no = item["TASK_PARAM1"].ToString();
                        string knife_number = item["TASK_PARAM3"].ToString();
                        string u_startime = item["START_TIME"].ToString();
                        string u_endtime = item["END_TIME"].ToString();
                        string d_startime = "";
                        string is_timeout = "";
                        double timeout_duration = 0;

                        dic = new Dictionary<string, string>();
                        dic.Add("TASK_PARAM1", machine_no);
                        dic.Add("END_TIME", u_startime);
                        DataTable dt_d = MethodHelp.GetOutsideTableInfo("TMS0026", dic, con_t, con);//运单明细
                        if (dt_d.Rows.Count>0)
                        {
                             d_startime = dt_d.Rows[0]["START_TIME"].ToString();                                              
                        }

                        TimeSpan difference = DB.ToDateTime(u_endtime) - DB.ToDateTime(d_startime);
                        double total_duration = Math.Round(difference.TotalMinutes,2);

                        if (total_duration > 3.41)
                        {
                            is_timeout = "Y";
                            timeout_duration =Math.Round(total_duration - 3.41, 2);
                        }

                        dic = new Dictionary<string, string>();
                        dic.Add("knife_number", knife_number);
                        dic.Add("machine_no", machine_no);
                        dic.Add("total_duration", total_duration.ToString());
                        dic.Add("timeout_duration", timeout_duration.ToString());
                        dic.Add("is_timeout", is_timeout);
                        dic.Add("d_time", d_startime);
                        dic.Add("uend_time", u_endtime);
                        string strSQL = MethodHelp.GetTranSQL("TMS0025", dic, con);//转换删除现有量表SQL语句
                        if (strSQL == "")
                        {
                            strMsg = "TMS0025,Parameter conversion failed";
                        }
                        else
                        {
                            strSqlList.Add(strSQL.ToString());
                        }

                    }
                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con_t);
                    }
                }


            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.code = 0;
               
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
